package com.school.view;

import com.school.domain.Course;
import com.school.domain.Grade;
import com.school.domain.Teacher;
import com.school.proxy.MyProxy;
import com.school.service.TeacherService;

import java.util.List;
import java.util.Scanner;

public class TeacherView {
    private long tid;
    private final TeacherService teacherService = (TeacherService) MyProxy.getProxy(TeacherService.class, "127.0.0.1", 10000);

    Scanner scanner = new Scanner(System.in);

    public TeacherView() {
    }


    public void teacherView() {
        Scanner scanner = new Scanner(System.in);
        boolean flag = true;
        login();
        while (flag) {
            System.out.println("1.查看个人信息\t2.修改个人信息\t3.添加课程\t4.查询课程\t5.上传学生分数\t6.修改课程\t7.登出");
            int chose = scanner.nextInt();
            switch (chose) {
                case 1:
                    showInfo();
                    break;
                case 2:
                    changeInfo();
                    break;
                case 3:
                    addCourse();
                    break;
                case 4:
                    selectCourse();
                    break;
                case 5:
                    upGrade();
                    break;
                case 6:
                    changeCourse();
                    break;
                case 7:
                    flag = false;
                    break;
                default:
                    System.out.println("请输入正确选项！");
                    break;
            }
        }
    }

    public void login() {
        while (true) {
            System.out.println("请输入账户密码：");
            long id = scanner.nextLong();
            String pwd = scanner.next();
            if (teacherService.teacherLogin(id, pwd)){
                tid = id;
                break;
            }
            System.out.println("账户或密码错误！");
        }

    }

    public void showInfo() {
        System.out.println(teacherService.findById(tid));
    }

    public void changeInfo() {
        Teacher teacher = teacherService.findById(tid);
        System.out.print("输入姓名：");
        teacher.setName(scanner.next());
        System.out.print("输入密码：");
        teacher.setPwd(scanner.next());
        System.out.print("输入年龄：");
        teacher.setAge(scanner.nextLong());
        System.out.print("输入性别：");
        teacher.setSex(scanner.next());
        teacherService.update(teacher);
    }

    public void addCourse() {
        System.out.print("输入课程名：");
        String name = scanner.next();
        System.out.print("输入课程总人数：");
        long num = scanner.nextLong();
        System.out.print("输入所用教室编号：");
        long rid = scanner.nextLong();
        System.out.print("输入所用教材编号：");
        long tbid = scanner.nextLong();
        Course course = new Course(0, name, num, 0, rid, tbid, tid);
        if (teacherService.addCourse(course)) {
            System.out.println("增加课程成功！");
            return;
        }
        System.out.println("添加失败！");
    }

    public void selectCourse() {
        List<Course> courses = teacherService.findCourses(tid);
        for (Course cours : courses) {
            System.out.println(cours);
        }
    }

    public void upGrade() {
        System.out.println("输入学生编号：");
        long sid = scanner.nextLong();
        System.out.println("输入课程编号：");
        long coid = scanner.nextLong();
        System.out.println("输入分数：");
        double grade = scanner.nextDouble();
        if (teacherService.addGrade(new Grade(0, grade, sid, coid))) {
            System.out.println("添加成功！");
            return;
        }
        System.out.println("添加失败！");

    }

    public void changeCourse() {
        System.out.println("输入你想改的课程编号：");
        Course course = teacherService.findCourse(scanner.nextLong());
        if (course.getTid() != tid) {
            System.out.println("不是您的课程，无法修改");
            return;
        }
        System.out.println("课程名：");
        course.setName(scanner.next());
        System.out.println("班级人数");
        course.setNum(scanner.nextLong());
        teacherService.updateCourse(course);

    }
}

